/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.cms.portlet.render;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.w3c.dom.Element;

import cn.com.chinasoa.cms.portlet.action.FrameworkConfigWraper;

import com.x4ee.soa.portal.framework.core.BaseRequestFilter;
import com.x4ee.soa.portal.framework.core.FrameworkConfig;
import com.x4ee.soa.portal.framework.core.RenderConfig;
import com.x4ee.soa.portal.framework.core.RenderFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-8-5下午06:10:05
 */
public class ContentRenderFilter extends BaseRequestFilter implements
		RenderFilter {

	private FrameworkConfigWraper tFrameworkConfigWraper=null;
	public String process(RenderRequest renderRequest, RenderResponse renderResponse,
			RenderConfig renderConfig) throws PortletException, IOException {
		//URI Map
		Object uriMapObj = renderRequest.getPortletSession().getAttribute(FrameworkConfigWraper.FRAMEWORK_URI_MAP);
		if((uriMapObj!=null)&&(uriMapObj instanceof Map)){

		}else{
			Map frameworkUriMap = new HashMap();
			Map uriMap = FrameworkConfigWraper.getURIMapByRenderName(tFrameworkConfigWraper.getXmlConfig(),"ViewIndex");
			for(Iterator rIt = uriMap.keySet().iterator();rIt.hasNext();){
				Object keyObj = rIt.next();
				if((keyObj!=null)&&(keyObj instanceof String)){
					String key = (String)keyObj;
					Object valueObj = uriMap.get(keyObj);			
					if((valueObj!=null)&&(valueObj instanceof String)){
						String mValue = (String)valueObj;
						if(mValue.indexOf("jsp/")>-1){
							String tValue= mValue.substring(mValue.lastIndexOf("jsp/")+4, mValue.length());
							frameworkUriMap.put(key, tValue);
						}
					}
				}
			}
			renderRequest.getPortletSession().setAttribute(FrameworkConfigWraper.FRAMEWORK_URI_MAP,frameworkUriMap);
		}
		
		//Outlet Map
		Object outletMapObj = renderRequest.getPortletSession().getAttribute(FrameworkConfigWraper.FRAMEWORK_OUTLET_MAP);
		if((outletMapObj!=null)&&(outletMapObj instanceof Map)){
		}else{
			Map frameworkOutletMap = FrameworkConfigWraper.getOutletMapByRenderName(tFrameworkConfigWraper.getXmlConfig(),"ViewIndex");
			renderRequest.getPortletSession().setAttribute(FrameworkConfigWraper.FRAMEWORK_OUTLET_MAP,frameworkOutletMap);
		}
		//Caculate return urn;
		String frameworkUrn = renderRequest.getParameter(FrameworkConfigWraper.FRAMEWORK_URN_URN);
		if(frameworkUrn!=null){
			if(FrameworkConfigWraper.hasOutlet(frameworkUrn, tFrameworkConfigWraper.getXmlConfig(),"ViewIndex"))
				return frameworkUrn;
			else 
				return FrameworkConfigWraper.DEFAULT;
		}else{
			return FrameworkConfigWraper.DEFAULT;
		}
	}

	public void init(Element tXmlConfig, FrameworkConfig frameworkConfig) throws PortletException {
		super.init(tXmlConfig, frameworkConfig);
		tFrameworkConfigWraper = new FrameworkConfigWraper(tXmlConfig,frameworkConfig);
	}

}
